package blog

import (
	"ginblog/api/common"
	"ginblog/model"
	"ginblog/utils/errmsg"
	"github.com/gin-gonic/gin"
)

type Register struct {
	common.RegisterBase
}

func (reg *Register) DoRegister(c *gin.Context) {
	//用户名不能为空
	if !reg.IsSetUserName() {
		reg.RegisterFail(errmsg.REGISTER_USERNAME_IS_NULL, c)
		return
	}
	//用户密码不能为空
	if !reg.IsSetPassword() {
		reg.RegisterFail(errmsg.REGISTER_PASSWORD_REQUEST, c)
		return
	}
	//手机号、邮箱至少有一个
	if !(reg.IsSetPhone() || reg.IsSetEmail()) {
		reg.RegisterFail(errmsg.REGISTER_INPUT_INCOMPLETE, c)
		return
	}
	Blog := model.Blog{UserName: reg.UserName}
	Blog.PassWord = Blog.PassWordEncrypt(reg.Password)
	//是否是手机号注册
	if reg.IsSetPhone() {
		Blog.Phone = reg.Phone
	}
	//是否是邮箱注册
	if reg.IsSetEmail() {
		Blog.Email = reg.Email
	}
	//去做注册
	if !Blog.PhoneOREmailRegister() {
		reg.RegisterFail(Blog.GetCode(), c)
		return
	}
	reg.RegisterSuccess(c)
	return
}
